package com.jmu.edu.IMApplication.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.google.android.material.button.MaterialButton;
import com.jmu.edu.IMApplication.MyApplication;
import com.jmu.edu.IMApplication.R;
import com.jmu.edu.IMApplication.bean.GestureAndFaceResult;
import com.jmu.edu.IMApplication.fragment.ReviseCustomTextFragment;
import com.jmu.edu.IMApplication.utils.ViewUtils;

import java.util.List;

public class GestureAndFaceResultItemAdapter extends RecyclerView.Adapter<GestureAndFaceResultItemAdapter.MyViewHolder> {
    private List<GestureAndFaceResult> gestureAndFaceResultList;

    public void setGestureAndFaceResultList(List<GestureAndFaceResult> gestureAndFaceResultList) {
        this.gestureAndFaceResultList = gestureAndFaceResultList;
    }

    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new MyViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_gesture_and_face_result, parent, false));
    }

    @Override
    public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
        GestureAndFaceResult result = gestureAndFaceResultList.get(position);
        holder.resultButton.setText(result.getIdentify().getChineseName());
        holder.resultButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String tag = "reviseCustomText";
                ReviseCustomTextFragment fragment = (ReviseCustomTextFragment) MyApplication.fragmentManager.findFragmentByTag(tag);
                if (fragment == null) {
                    fragment = new ReviseCustomTextFragment();
                }
                fragment.setResult(result);
                ViewUtils.showFragment(fragment, tag, R.anim.from_right, R.anim.out_right);
            }
        });
    }

    @Override
    public int getItemCount() {
        return gestureAndFaceResultList.size();
    }

    public static class MyViewHolder extends RecyclerView.ViewHolder {
        private MaterialButton resultButton;

        public MyViewHolder(@NonNull View itemView) {
            super(itemView);
            resultButton = itemView.findViewById(R.id.item_gesture_and_face_result);
        }
    }
}
